package com.debao;


import com.debao.common.core.controller.BaseController;
import com.debao.common.core.domain.AjaxResult;
import com.debao.common.core.page.TableDataInfo;
import com.debao.common.utils.SecurityUtils;
import com.debao.system.domain.RefundRecord;
import com.debao.system.service.IRefundRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


/**
 * 退款记录
 */
@CrossOrigin
@RestController
@RequestMapping("/api/record")
public class ApiRecord extends BaseController {

    @Autowired
    private IRefundRecordService refundRecordService;

    /**
     * 获取我自己的退款订单列表
     *
     * @param refundRecord
     * @return
     */
    @GetMapping("/recordList")
    public TableDataInfo recordList(RefundRecord refundRecord) {
        startPage();
        refundRecord.setUserId(SecurityUtils.getUserId());
        List<RefundRecord> list = refundRecordService.selectRefundRecordList(refundRecord);
        return getDataTable(list);
    }

    /**
     * 申请退款
     */
    @PostMapping("/applyRefund")
    public AjaxResult applyRefund(@RequestBody RefundRecord refundRecord) {
        refundRecord.setUserId(SecurityUtils.getUserId());
        int row = refundRecordService.insertRefundRecord(refundRecord);
        if (row == 1) {
            return AjaxResult.success("退款申请成功，等待审核");
        }
        return AjaxResult.error("退款申请失败");
    }

    /**
     * 撤销退款
     */
    @PostMapping("/cancelRefund")
    public AjaxResult cancelRefund(@RequestBody RefundRecord refundRecord) {
        refundRecord.setUserId(SecurityUtils.getUserId());
        int row = refundRecordService.updateRefundRecord(refundRecord);
        if (row == 1) {
            return AjaxResult.success("撤销退款成功");
        }
        return AjaxResult.error("撤销退款失败");
    }
}